package com.yupi.yupao.once;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yupi.yupao.model.domain.User;
import com.yupi.yupao.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


// 有个很重要的点 TableListener 不能被spring管理，
// 要每次读取excel都要new,然后里面用到spring可以构造方法传进去
@Slf4j
@Component
public class TableListener implements ReadListener<XingQiuTableUserInfo> {
    @Autowired
    private UserService userService;

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data    one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(XingQiuTableUserInfo data, AnalysisContext context) {
        System.out.println(data);
        //利用监听器读取的信息保存到数据库中
        String planetCode = data.getPlanetCode();
        String username = data.getUsername();
        User user = new User();
        user.setPlanetCode(planetCode);
        user.setUsername(username);
        User safetyUser = userService.getSafetyUser(user);//用户脱敏
        //用户保存
        userService.save(safetyUser);
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("已解析完成");
    }

}